<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Home\UserinfoRequest;
use App\Http\Requests\Home\UpdatepassRequest;
use App\Http\Requests\Home\UpdatephoneRequest;
use App\Http\Requests\Home\UpdateemailRequest;
use App\Models\User;
use App\Models\Userinfo;
use Hash;
use Mail;

class UserinfoController extends Controller
{
    //加载个人资料页
    public function getIndex()
    {
        //
        $uid = session('user')['uid'];
        $user = User::find($uid);
        $userinfo = Userinfo::where('uf_uid',$uid)->first();
        return view('home.userinfo',['title'=>'个人资料','user'=>$user,'userinfo'=>$userinfo]);
    }

    public function postUpdate(UserinfoRequest $request)
    {
        //
        $data = $request->except('_token','_method');
        $id = $data['uid'];
        $user=User::find($id);
        $userinfo = Userinfo::where('uf_uid',$id)->first();
        $uname = $data['uname'];
        //查询用户名是否和自己的邮箱、手机相同
        $res1 = ($user->uname == $uname);
        $res2 = ($user->uphone == $uname);
        $res3 = ($user->uemail == $uname);
        //查询用户名是否和库里的用户名邮箱、手机相同
        $res4 = User::where('uname',$uname)->orWhere('uphone',$uname)->orWhere('uemail',$uname)->first();
        //用户名只能和自己的邮箱手机相同，能不能和别人的相同
        if($res1==false && $res2==false && $res3==false && $res4 == true){
            return back()->with('errors','用户名已存在');
        }else{
           $user->uname = $uname; 
        }
        
        $userinfo->ufname = $data['ufname'];
        $userinfo->ufsex = $data['ufsex'];
        $userinfo->ufbirthday= $data['ufbirthday'];
        if($user->save() && $userinfo->save()){
            return back()->with('success','修改成功');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    public function getSafety()
    {
        $uid = session('user')['uid'];
        $user = User::find($uid);
        $userinfo = Userinfo::where('uf_uid',$uid)->first();
        return view('home.safety',['title'=>'安全设置','user'=>$user,'userinfo'=>$userinfo]);
    }

    public function getEditpass()
    {
        return view('home.editpass',['title'=>'修改密码']);
    }

    public function postUpdatepass(UpdatepassRequest $request)
    {
        // dd($request->all());
        $data = $request->all();
        $oldpassword = $data['oldpassword'];
        $uid = $data['uid'];
        $user = User::find($uid);
        if(!Hash::check($oldpassword, $user->upassword)){
            return back()->with('errors','原密码错误');
        }
        $user->upassword = Hash::make($data['upassword']);
        if($user->save()){
            return back()->with('success','修改成功');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    public function getEditphone()
    {
        return view('home.editphone',['title'=>'绑定手机']);
    }

    public function postUpdatephone(UpdatephoneRequest $request)
    {
        $data = $request->all();
        $phone = $data['uphone'];
        $phonecode = \Redis::get('STRING:PHONE:CODE:'.$phone);
        if($data['phonecode']!=$phonecode){
            return back()->with('errors','验证码错误')->withinput();
        }
        $uid = $data['uid'];
        $user = User::find($uid);
        $user->uphone = $phone;
        if($user->save()){
             return redirect('/home/userinfo/safety')->with('success','绑定成功');
        }else{
             return back()->with('errors','绑定失败，请重试');
        }
    }

        //发送手机验证码
    public function getPhonecode(Request $request){
        $phone = $request->input('phone');
        //检验是否重复发送
        $exists = \Redis::exists('STRING:PHONE:CODE:'.$phone);
        if(!empty($exists)){
            return 1;
        }
        $code = rand(1000,9999);
        $url = 'http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C03435536&password=758eae53a6da145d32f8e026850cd5b3&format=json&mobile='.$phone.'&content=您的验证码是：'.$code.'。请不要把验证码泄露给其他人。';
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        // 将json转化为数组
        $arr = json_decode($res,true);
        //$arr['code']==2 代表发送成功
        if($arr['code']==2){
            //防止重复发送
            \Redis::setex('STRING:PHONE:CODE:'.$phone,60,$code);
            return 2;
        }else{
            return 0;
        }
    }

    //ajax验证手机号是否存在
    public function getAjaxphone(Request $request)
    {
        $phone = $request['uphone'];
        $res = User::where('uphone',$phone)->orWhere('uname',$phone)->first();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    public function getEditemail()
    {
        return view('home.editemail',['title'=>'绑定邮箱']);
    }

    //ajax验证邮箱是否存在
    public function getAjax(Request $request)
    {
        $email = $request['uemail'];
        $res = User::where('uemail',$email)->orWhere('uname',$email)->first();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    public function getEmail(Request $request)
    {
        $uemail = $request['uemail'];
        //检验是否重复发送
        $exists = \Redis::exists('STRING:EMAIL:CODE:'.$uemail);
        if(!empty($exists)){
            return 0;
        }
        $code = rand(1000,9999);
        $res = Mail::send('home.emailcode',['code'=>$code],function($m) use ($uemail){
            $m->to($uemail)->subject('Mall 绑定邮箱!');
        });
        if($res){
            \Redis::setex('STRING:EMAIL:CODE:'.$uemail,60,$code);
            return 1;   
        }else{
            return 2;
        }
        
    }

    public function postUpdateemail(UpdateemailRequest $request)
    {
        $data = $request->all();
        $uemail = $data['uemail'];
        $code = \Redis::get('STRING:EMAIL:CODE:'.$uemail);
        if($data['code']!=$code){
            return back()->with('errors','验证码错误')->withinput();
        }
        $uid = $data['uid'];
        $user = User::find($uid);
        $user->uemail = $uemail;
        if($user->save()){
             return redirect('/home/userinfo/safety')->with('success','绑定成功');
        }else{
             return back()->with('errors','绑定失败，请重试');
        }
    }
}